package com.photoeditor.demo.ui.widget.image.mixer;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.ui.widget.image.RoundImageView;
import com.photoeditor.R;
import com.photoeditor.demo.app.BaseRecycleViewAdapter;
import com.photoeditor.demo.util.FontUtil;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by winn on 17/12/29.
 */

public class MixerModeAdapter extends BaseRecycleViewAdapter<MixerMode> {

    private int mSelItemIndex = -1;
    private WeakReference<View> mCurSelViewBg;//bg
    private WeakReference<View> mCurSelViewIcon; // check

    @Override
    protected RecyclerView.ViewHolder createVHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.item_mixermodelist, parent, false);
        ModeViewHoler viewHolder = new ModeViewHoler(layout);
        return viewHolder;
    }

    @Override
    protected void showItemView(RecyclerView.ViewHolder viewholder, int position) {
        ModeViewHoler holder = (ModeViewHoler) viewholder;
        final MixerMode data = mData.get(position);
        holder.mItemPosition = position;
        holder.mIcon.setImageBitmap(null);
        holder.mIcon.setImageResource(data.getDrawable());
        holder.mName.setText(data.getName());
        if (mSelItemIndex == position) {
            holder.mSelectBackground.setVisibility(View.VISIBLE);
            mCurSelViewBg = new WeakReference<>(holder.mSelectBackground);
            holder.mIconBg.setVisibility(View.VISIBLE);
            mCurSelViewIcon = new WeakReference<>(holder.mIconBg);
        } else {
            holder.mSelectBackground.setVisibility(View.GONE);
        }
    }

    @Override
    public long getAdapterItemId(int position) {
        return 0;
    }

    public class ModeViewHoler extends RecyclerView.ViewHolder {
        public RoundImageView mIcon;
        public TextView mName;
        public View mIconBg;
        public View mSelectBackground;
        public int mItemPosition;

        public ModeViewHoler(View itemView) {
            super(itemView);
            mIcon = itemView.findViewById(R.id.iv_icon);
            mName = itemView.findViewById(R.id.tv_name);
            FontUtil.setCustomFont(mName);
            mIconBg = itemView.findViewById(R.id.select_bg);
            mSelectBackground = itemView.findViewById(R.id.select_background);
        }
    }

    /**
     * 在onitemClick中调用
     *
     * @param seletcItemPosition
     * @param view
     */
    public void setSeletcItemPosition(int seletcItemPosition, View view) {
        mSelItemIndex = seletcItemPosition;
        if (mCurSelViewBg != null && mCurSelViewBg.get() != null) {
            mCurSelViewBg.get().setVisibility(View.GONE);
        }
        mCurSelViewBg = new WeakReference<>(view.findViewById(R.id.select_background));
        view.findViewById(R.id.select_background).setVisibility(View.VISIBLE);

        if (mCurSelViewIcon != null && mCurSelViewIcon.get() != null) {
            mCurSelViewIcon.get().setVisibility(View.GONE);
        }
        mCurSelViewIcon = new WeakReference<>(view.findViewById(R.id.select_bg));
        view.findViewById(R.id.select_bg).setVisibility(View.VISIBLE);
    }

    public void setSeletcItemPosition(int seletcItemPosition) {
        mSelItemIndex = seletcItemPosition;
        if (mCurSelViewBg != null && mCurSelViewBg.get() != null) {
            mCurSelViewBg.get().setVisibility(View.GONE);
        }

        if (mCurSelViewIcon != null && mCurSelViewIcon.get() != null) {
            mCurSelViewIcon.get().setVisibility(View.GONE);
        }
    }

    public int getSeletcItemPosition() {
        return mSelItemIndex;
    }

    public int getPositionByMode(MixerMode mode) {
        int ret = -1;
        List<MixerMode> data = getData();
        if (data != null) {
            for (int i = 0; i < data.size(); ++i) {
                MixerMode tmp = data.get(i);
                if (mode.getMode() == tmp.getMode()) {
                    ret = i;
                    break;
                }
            }
        }
        return ret;
    }
}
